package com.platform.cdcs.fragment.stock;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.fragment.account.AccountListFragment;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.DistCustomerList;
import com.platform.cdcs.model.HouseItem;
import com.platform.cdcs.model.MockObj;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.ViewTool;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.sourceforge.zbar.Image;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * Created by holytang on 2017/11/18.
 */
public class StockDetailFragment extends BaseFragment {

    private String id;
    private View rootView;
    private EditText addressET, remarkET, codeET, nameET;
    private ImageView setImg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    @Override
    public void initView(View view) {
        initLoadImg(view.findViewById(R.id.load));
        setTitle("库位详情");
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        rootView = view;
        rootView.findViewById(R.id.text).setVisibility(View.GONE);
        requestDetail();
    }

    private void requestDetail() {
        showLoadImg();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        getHttpClient().post().url(Constant.DIST_WHHOUSE_INFO).params(Constant.makeParam(map)).build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    if (getContext() == null) {
                        return;
                    }
                    dismissLoadImg();
                    Type type = new TypeToken<BaseObjResponse<HouseItem.HouseList>>() {
                    }.getType();
                    BaseObjResponse<HouseItem.HouseList> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        setData(response.getResult().getDistWhHouseInfo());
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {
                    Utils.showToast(getContext(), "解析数据失败");
                }
            }
        });
    }

    private void setData(HouseItem model) {
        TextView btn1 = (TextView) rootView.findViewById(R.id.text);
        btn1.setVisibility(View.VISIBLE);
        btn1.setText("修改");
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editPost();
            }
        });
        TextView btn2 = (TextView) rootView.findViewById(R.id.text1);
        btn2.setBackgroundResource(R.drawable.btn_red);
        btn2.setText("删除");
        btn2.setVisibility(View.VISIBLE);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deletePost();
            }
        });
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);
        codeET = ViewTool.setFourItemEdit(inflater, root1, new String[]{"库位代码", model.getWhCode()});
        nameET = ViewTool.setFourItemEdit(inflater, root1, new String[]{"库位名称", model.getWhName()});
        addressET = ViewTool.setFourItemEdit(inflater, root1, new String[]{"库位地址", model.getWhAddress()});
        ViewTool.setFourItem(inflater, root1, new String[]{"保存时间", "更新时间"}, new String[]{model.getFill_date(), model.getUpdate_date()});

        LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
        setImg = ViewTool.createSwitchItem(inflater, root2, "设为主库", true, false);
        setImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (setImg.isSelected()) {
                    setImg.setSelected(false);
                } else {
                    setImg.setSelected(true);
                }
            }
        });
        if("1".equals(model.getIsMainHouse())){
            setImg.setSelected(true);
        }
        remarkET = ViewTool.setFourItemEdit(inflater, root2, new String[]{"备注", model.getRemark()});
        rootView.findViewById(R.id.button3).setVisibility(View.GONE);
        rootView.findViewById(R.id.button4).setVisibility(View.GONE);
        rootView.findViewById(R.id.button5).setVisibility(View.GONE);

    }

    @Override
    public int layoutId() {
        return R.layout.five_layout;
    }


    private void editPost() {
        if (TextUtils.isEmpty(nameET.getText().toString().trim())) {
            Utils.showToast(getContext(), "库位名称不能为空!");
            return;
        }
        showLoadImg();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("updateType", "0");
        map.put("whCode", codeET.getText().toString().trim());
        map.put("whName", nameET.getText().toString().trim());
        map.put("whAddress", addressET.getText().toString().trim());
        map.put("remark", remarkET.getText().toString().trim());
        map.put("isMainHouse", setImg.isSelected() ? "1" : "0");

        getHttpClient().post().url(Constant.UPDATE_DIST_WHHOUSEINFO).params(Constant.makeParam(map)).build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    dismissLoadImg();
                    Type type = new TypeToken<BaseObjResponse<MockObj>>() {
                    }.getType();
                    BaseObjResponse<MockObj> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        Utils.showToast(getContext(), "修改成功！");
                        RefershEvent event = new RefershEvent();
                        event.mclass = StockListFragment.class;
                        EventBus.getDefault().post(event);
                        getActivity().finish();
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {
                    Utils.showToast(getContext(), "解析数据失败");
                }
            }
        });

    }


    private void deletePost() {
        showLoadImg();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("updateType", "1");
        getHttpClient().post().url(Constant.UPDATE_DIST_WHHOUSEINFO).params(Constant.makeParam(map)).build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    dismissLoadImg();
                    Type type = new TypeToken<BaseObjResponse<MockObj>>() {
                    }.getType();
                    BaseObjResponse<MockObj> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        Utils.showToast(getContext(), "删除成功！");
                        RefershEvent event = new RefershEvent();
                        event.mclass = StockListFragment.class;
                        EventBus.getDefault().post(event);
                        getActivity().finish();
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {
                    Utils.showToast(getContext(), "解析数据失败");
                }
            }
        });
    }
}
